package com.teach.sys.service.impl;

import com.teach.AuthUser;
import com.teach.CodeException;
import com.teach.sys.config.UserProperties;
import com.teach.sys.dto.UserRegDto;
import com.teach.sys.entity.User;
import com.teach.sys.mapper.UserMapper;
import com.teach.sys.service.UserService;
import com.teach.util.PasswordUtil;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author J.L.Zhou
 * @since 2022-10-22 20:39
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

  @Resource
  private PasswordUtil passwordUtil;

  @Override
  public void resetPwd(Integer userId, String userPwd) {
    //后端校验 vs 前端校验
    //后端校验框架 || 手工校验
    if(StrUtil.isBlank(userPwd)){
      throw new CodeException(1, "密码必填");
    }
    if(!userPwd.matches("^.{2,10}$")){
      throw new CodeException(2, "密码有2-10个字符组成");
    }
    User user = baseMapper.selectById(userId);
    if(user==null){
      throw new CodeException(3, "用户不存在");
    }
    userPwd = passwordUtil.encode(userPwd,user.getUserTel(), 3);//登录,注册,修改密码 加密方式必须一致
    User u = new User();
    u.setUserId(userId);
    u.setUserPwd(userPwd);
    baseMapper.updateById(u);
  }


  @Autowired
  private UserProperties userProperties;

  @Override
  public User reg(UserRegDto userRegDto) {
    log.trace("注册出入:{}",userRegDto);
    log.trace("用户默认值:{}", userProperties);

    //TODO: 用户注册: 手工校验: 手机号码存在性(唯一)
    long count = baseMapper.selectCount(where().eq(User::getUserTel, userRegDto.getUserTel()));
    if(count>0){
      throw new CodeException(1,"手机号码已经存在");
    }

    //TODO: 密码加密
    userRegDto.setUserPwd(passwordUtil.encode(userRegDto.getUserPwd(),userRegDto.getUserTel(), 3));

    User user = new User();
    // user.setUserName(userRegDto.getUserName());
    // user.setUserTel(user.getUserTel());
    BeanUtils.copyProperties(userRegDto, user);
    BeanUtils.copyProperties(userProperties, user);
    baseMapper.insert(user);
    log.trace("注册用户:{}", user);
    return user;
  }

  private LambdaQueryWrapper<User> where(){
    return new LambdaQueryWrapper<>();
  }

  @Override
  public AuthUser login(String userTel, String userPwd) throws CodeException {
    //TODO: 用户登录-01-根据用户手机号码获取用户信息
    User user = baseMapper.selectOne(where().eq(User::getUserTel, userTel));
    //TODO: 用户登录-02-返回的用户信息为空,则抛出异常
    if(user==null) {
      throw new CodeException(1, "用户未找到");
    }
    //TODO: 用户登录-03-输入的密码加密后和用户密码比较,如果不一致则抛出异常
    if(!user.getUserPwd().equals(passwordUtil.encode(userPwd,userTel, 3))){
      throw new CodeException(2, "密码错误");
    }
    //TODO: 用户登录-04-判断用户的锁定状态如果为true,则抛出异常
    if(user.getUserLocked()){
      throw new CodeException(3, "用户被锁定");
    }
    //TODO: 用户登录-05-未抛出异常,则用户登录验证成功,下面将构建AuthUser对象,并返回
    AuthUser authUser = new AuthUser();
    authUser.setRoleId(user.getRoleId());
    authUser.setUserId(user.getUserId());
    return authUser;
  }

}
